<template>
  <a-modal
    :title="isEdit ? '修改盘点单' : '添加盘点单'"
    :visible.sync="topAddShow"
    :confirm-loading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    :maskClosable="false"
    width="30%"
    class="basic-modal"
  >
    <div class="title">
      <a-form-model
        :label-col="{ span: 6 }"
        :wrapper-col="{ span: 14 }"
        :model="essenceItem"
        ref="form"
      >
        <a-form-model-item label="盘点单号">
          <a-input v-model="essenceItem.InventoryOrderNo" placeholder="不填写自动生成" />
        </a-form-model-item>
        <a-form-model-item label="计划盘点时间">
          <a-date-picker
            format="YYYY-MM-DD"
            v-model="essenceItem.PlanDt"
            style="width: 100%"
          />
        </a-form-model-item>
        <a-form-model-item label="盘点厂区">
          <a-select
            class="selectwidth"
            v-model="essenceItem.WSType"
            placeholder="请选择"
            @change="handleChangeWSType"
          >
            <a-select-option
              v-for="item in WSTypeList"
              :key="item.type"
              :value="item.type"
              >{{ item.name }}</a-select-option
            >
          </a-select>
        </a-form-model-item>
        <a-form-model-item label="盘点区域">
          <a-select
            class="selectwidth"
            :disabled="essenceItem.WSType == null"
            v-model="essenceItem.WSSegmentID"
            placeholder="请选择"
          >
            <a-select-option
              v-for="item in QSegmentList"
              :key="item.type"
              :value="item.type"
              >{{ item.name }}</a-select-option
            >
          </a-select>
        </a-form-model-item>
        <a-form-model-item label="备注">
          <a-textarea style="resize: none" v-model="essenceItem.Remark" />
        </a-form-model-item>
      </a-form-model>
    </div>
  </a-modal>
</template>

<script>
import {
  insInventoryOrder,
  updInventoryOrder,
} from "@/api/InventoryOrder/index";

import { GetWorkShopTypesAll } from "@/api/WorkShopType/index";
import { GetSegments } from "@/api/NBCSegment/index";
export default {
  props: {
    topAddShow: {
      type: Boolean,
      default: false,
    },
    essenceItem: {
      type: Object,
      default: {},
    },
    isEdit: {
      type: Boolean,
      default: false,
    },
  },
  model: {
    prop: "topAddShow",
    event: "close",
  },

  data() {
    return {
      boottleShow: false,
      feedShow: false,
      confirmLoading: false, //modal的loading
      WSTypeList: [], //厂区列表
      QSegmentList: [], //站点列表
    };
  },
  methods: {
    /** 选中厂区 */
    handleChangeWSType(value) {
      let that = this;
      that.essenceItem.WSSegmentID = null;
      that.GetSegmentsAllFun(that.essenceItem.WSType, true);
    },

    /** 获取厂区列表 */
    async GetWorkShopTypesAllFun() {
      let that = this;
      let res = await GetWorkShopTypesAll(10);
      if (res.Status == 10000) {
        let wtList = [];
        res.Data.map((item) => {
          wtList.push({ type: item.WSType, name: item.WSName });
        });
        that.WSTypeList = wtList;
      } else {
        that.$message.error(res.Message);
      }
    },

    //获取车间列表
    async GetSegmentsAllFun(WSType) {
      let that = this;
      let SegSegmentID = [
        "67CE6CD3-0E52-4284-AAFC-E20869415EAA",
        "AE6D566D-D22F-4280-9A14-9375B915AFD0",
      ];
      let res = await GetSegments({
        SegSegmentID: SegSegmentID,
        wsTypeList: [WSType],
      });
      if (res.Status == 10000) {
        let wtList = [];
        res.Data.map((item) => {
          wtList.push({ type: item.SegmentID, name: item.SegmentName });
        });
        that.QSegmentList = wtList;
      } else {
        that.$message.error(res.Message);
      }
      that.spinning = false;
    },

    /** 新增盘点单预分配 */
    async insData() {
      try {
        let { Data } = await insInventoryOrder(this.essenceItem);
        this.$message.success("添加成功");
        this.handleCancel();
        this.$emit("up-data");
      } catch (e) {
        this.$message.error(e);
      }
    },

    /** 修改盘点单预分配 */
    async upData() {
      try {
        let { Data } = await updInventoryOrder(this.essenceItem);
        this.$message.success("修改成功");
        this.handleCancel();
        this.$emit("up-data");
      } catch (e) {
        this.$message.error(e);
      }
    },

    //关闭弹窗
    handleCancel() {
      this.$emit("close", false);
    },
    //确认添加/修改
    handleOk() {
      if (!this.essenceItem.WSType)
        return this.$message.error("请先选择盘点厂区");
      if (!this.essenceItem.WSSegmentID)
        return this.$message.error("请先选择盘点区域");
      if (!this.isEdit) {
        this.insData();
      } else {
        this.upData();
      }
    },

    /** 选择灌装单 */
    boottleFun() {
      this.boottleShow = true;
    },

    /** 灌装单赋值 */
    orderItemFun(item) {
      this.essenceItem.PackingPlanID = item.PackingPlanID;
      this.formItem.packingPlanNo = item.PackingPlanNo;
    },

    /** 选择投料单 */
    feedFun() {
      this.feedShow = true;
    },

    /** 投料单赋值 */
    feedtemFun(item) {
      this.essenceItem.PlanNos = item.expandedRowKeys;
      this.formItem.planNoText = item.expandedRowKeys.join(",");
    },
    //表格选择
    handleTableChange() {},
  },
  watch: {
    topAddShow: {
      handler(newV, oldV) {
        if (newV) {
          if (this.isEdit) {
            this.GetWorkShopTypesAllFun();
            this.GetSegmentsAllFun(this.essenceItem.WSType);
          } else {
            this.GetWorkShopTypesAllFun();
          }
        }
      },
      immediate: true, // 重点
      deep: true, // 重点
    },
  },
};
</script>

<style lang="less" scoped></style>
